<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class ResourceController extends Controller {
		public function index(){
			$start = Basic::getVar("start");
			$perpage = Basic::getVar("perpage");
			$search = Basic::getVar("search");
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			$searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
			$sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"name";
			$sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
			$resource = $this->loadModel("Resource");
			$resourceRecords = $resource->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
			$totalRecords = $resource->getTotalRecords();
			if ( $start < $totalRecords ){
				$this->set("totalRows",$totalRecords);
			}elseif ( $start >= $totalRecords ) {
				$start = $start-20;
				$resourceRecords = $resource->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
				$this->set("totalRows",$totalRecords);
			}
			$this->set("currentPage",$start);
			$this->set("perpage",$perpage);
			$this->set("search",$search);
			$this->set("searchColumns",$searchColumns);
			$this->set("sortColumn",$sortColumn);
			$this->set("sortCriteria",$sortCriteria);
			$this->set("search",$search);
			$this->set("resources",$resourceRecords);
			$this->set("title","SMECA - Resource List");
			$this->set("selectedMenu","RES");
			$this->setLanguage( "English" );
			$this->renderDefault("display","resource");
		}
		public function store(){
			Import::AppLib("word");
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
			
			$article = $this->loadModel( "Article" );
			$banner = $this->loadModel( "Banner" );
			
			$menu = $this->loadModel("Menu");
			$menus = $menu->getMenu("Section Menues","en");
			$footer_menus = $menu->getMenu("Section Footer","en");
			
			//$firstMenu = $menus[0];
			//$this->set("selectMenu",$firstMenu->title_en);
			$this->set("selectMenu","");
			$this->set('menus',$menus);
			$this->set('footerMenues',$footer_menus);
			
			$this->set("lastArticles",$article->getLatestArticle() );
			$this->set("topArticles",$article->getLatestArticleByHit() );
			
			$this->set("topBanners",$banner->topBanners());
			$this->set("rightBanners",$banner->rightBanners());
			$this->set("bottomBanners",$banner->bottomBanners());
			
			$resource = $this->loadModel("Resource");
			//$type = isset($_SESSION["user"])?($_SESSION["user"]->type!=2?array(3):array(2,3)):array(3);
			$type = array(2,3) ;
			$catId = Basic::getVar("catid");
			if($catId)$this->set("category",$resource->getCategory($catId));
			$categories = $resource -> getCategories($catId,$_SESSION["lang"]);
			$this->set('categories', $categories);
			$this->set("resources" ,$resource->getPublished($type,$catId,$_SESSION['lang']));
			
			$this->set( "title" , "SMECA - " . $categories[0] -> menu_name );
			$this->renderDefault("store","resource");
		}
		public function add(){
			$category = $this->loadModel("Category");
			$this->set("selectedMenu","RES");
			$this->set( "title" , "SMECA - Resource New" );
			$this->set("menus", $category->getCategory(22));
			$this->setLanguage( "English" );
			$this->renderDefault("add","resource");
		}
		public function edit(){
			$resource_id = Basic::getVar("id");
			if($resource_id){
				$category = $this->loadModel("Category");
				$resource = $this->loadModel("Resource");
				$this->set("selectedMenu","RES");
				$this->set( "title" , "SMECA - Resource Edit" );
				$this->set("menus", $category->getCategory(22));
				$this->set("resource",$resource->find($resource_id));
				$this->setLanguage( "English" );
				$this->renderDefault("edit","resource");
			}else{
				$this->redirect(array("com"=>"resource","action"=>"index"));
			}
		}
		public function editprocess(){
			$id = Basic::getVar("id");
			$type = Basic::getVar("resource_type");
			$description_kh = Basic::getVar("content_kh");
			$description_en = Basic::getVar("content_en");
			$published = Basic::getVar("published");
			$caption = Basic::getVar("caption");
			$menu_id = Basic::getVar("menu_id");
			$user = SESSION::get("user");
			$resource = $this->loadModel("Resource");
			if ( $id && $caption && $menu_id && $type !== false ){
				$newname = $resource->edit($id , $user->id , $caption , $type , $menu_id , $description_kh, $description_en , $published);
			}
			$this->redirect(array("com"=>"resource","action"=>"index"));
		}
		public function addprocess(){
			$type = Basic::getVar("resource_type");
			$description_kh = Basic::getVar("content_kh");
			$description_en = Basic::getVar("content_en");
			$published = Basic::getVar("published");
			$caption = Basic::getVar("caption");
			$menu_id = Basic::getVar("menu_id");
			$user = SESSION::get("user");
			$resource = $this->loadModel("Resource");
			$result=Basic::getFileInfo("resource_file",25);

			if ($caption != "" && $menu_id != "" && $result["name"] != "" ){
				$newname = $resource->add($user->id , $caption , str_replace(" ","-",$result["name"]) , $type , $menu_id , $result["size"] , $result["type"] , $description_kh, $description_en , $published);
				Basic::uploadFile("resource_file",$newname,25,"resources");
			}
			$this->redirect(array("com"=>"resource","action"=>"index"));
		}
		public function published(){
			$id = Basic::getVar("id");
			$published = Basic::getVar("publish");
			if ( $id && $published !== false ){
				$resource = $this->loadModel("Resource");
				$resource->setPublished($id,$published);
				echo 1;
			}else{
				echo 0;
			}
		}
		public function delete(){
			$id = Basic::getVar("id");
			if ( $id ){
				$resource = $this->loadModel("resource");
				$resourceData = $resource->find($id);
				$resourceData = $resourceData[0];
				if ( Basic::removeFileOrDir("resources/".$resourceData->id) ){
					$resource->delete($id);
				}
			}
			$this->redirect(array("com"=>"resource","action"=>"index"));
		}
		public function download(){
			$resource = $this->loadModel("resource");
			$resource_id = Basic::getVar("id");
			$record = $resource->find($resource_id);
			if(count($record)>0){
				$record = $record[0];
				@header("Content-Length: ". $record->size );
				@header("Content-Type: " . $record->type );
				@header("Content-Disposition: attachment; filename=" . str_replace(" ","-",$record->name) );
				echo file_get_contents("resources/".$record->id);
			}
		}
	}